//
//  HotShopCollectionView.m
//  Experss
//
//  Created by 嗯，大葱 on 17/7/28.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "HotShopCollectionView.h"
#import "HotShopCell.h"
#import "HotShopModel.h"

@implementation HotShopCollectionView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _dataAray = [NSMutableArray array];
        [self configureView];
        [self getHotShopData];
    }
    return self;
}

- (void)configureView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    //设置分区的缩进量;
    layout.sectionInset = UIEdgeInsetsMake(5,5, 5, 5);
    //设置行间距;t
    layout.minimumLineSpacing = 5;
    //设置最小的左右间距
    layout.minimumInteritemSpacing = 0;
    //设置滑动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width , self.frame.size.height) collectionViewLayout:layout];
    //dataSource -- 提供数据;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_collectionView];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView registerNib:[UINib nibWithNibName:@"HotShopCell" bundle:nil] forCellWithReuseIdentifier:@"HotShopCell"];
    
}
#pragma mark --- dataSource 方法;
//设置分区的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
//设置每个分区Item的个数 -- 相当于表视图设置每个分区行数;
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataAray.count;
}
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(120, 185);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotShopCell" forIndexPath:indexPath];
    
    HotShopModel *model = self.dataAray[indexPath.row];
    [cell.logoV sd_setImageWithURL:[NSURL URLWithString:model.logo]];
    cell.nameLB.text = model.shop_name;
    cell.intoShopBtn.tag = [model.m_id integerValue]+1000;
    [cell.intoShopBtn addTarget:self action:@selector(handleInToHotShopBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(chooseCollectionIndepath:)]) {
        [self.delegate chooseCollectionIndepath:indexPath];
    }
    
}

//获取热门商铺
- (void)getHotShopData {
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    [HttpRequest getWithURLString:POST(Shop_HomeGetHomeHotMemberShop) parameters:parameDic success:^(id responseObject) {
        if ([responseObject[@"ret"] intValue] != 200) {
            [[DSLHUD shareDSLHUD] showTextNoActivity:@"获取热门商铺出错" timeLength:1.5];
            return ;
        }
        NSArray *dataArray = responseObject[@"data"][@"info"];
        NSLog(@"--=-=-=-=%@",responseObject);
        
        for (NSDictionary *tempD in dataArray) {
            HotShopModel *model = [[HotShopModel alloc] init];
            [model setValuesForKeysWithDictionary:tempD];
            [self.dataAray addObject:model];
        }
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
}

//进入热门商铺按钮的点击事件
- (void)handleInToHotShopBtnAction:(UIButton *)sender {

    if(self.delegate && [self.delegate respondsToSelector:@selector(intoHotShopBtnAction:shopId:)])
    {
        [self.delegate intoHotShopBtnAction:sender shopId:[NSString stringWithFormat:@"%ld",sender.tag - 1000]];
    }
    
}
@end
